## Table H.3: Table H.3: Firm and Employee Donations and Lobbying of Specific 
#Politicians, First-Time-Ever Donation, Full Results: --------------------------

## Instructions ----------------------------------------------------------------
# Step 1: Adjust MAIN_DIR to main folder
# Step 2: Adjust DATA_DIR to where data is is located
# Step 3: Adjust FIG_DIR to where output folder is
# Step 4: Run entire script
## setup -----------------------------------------------------------------------

# clean slate
rm(list = ls())

# load packages
library(dplyr)
library(stargazer)
library(lfe)
library(tidyr)
options(scipen=999)

# set directory
MAIN_DIR <- "C:/Users/js.egb/Dropbox/campaign-lobby-paper/replication_package"

# set data folder
DATA_DIR <-  paste(MAIN_DIR, "/data/", sep = "")

# set output folder
FIG_DIR <- paste(MAIN_DIR, "/output/", sep = "")


#subset to manufacturing industries
naics_sub <- c("31", "32", "33") # set industries to be analyzed

# file to be read in
#file_in <- paste0(DATA_DIR, "don_lob_all_ind_20210203.csv") # file to be input
file_in <- paste0(DATA_DIR, "don_lob_all_ind_20210203.rds") # file to be input


# read data
don_lob <- readRDS(file_in) %>%
  filter(naics2 %in% naics_sub) %>%
  mutate(l_sale = log(sale+1), 
         l_emp = log(emp+1), 
         l_don_tot = log(don_tot+1)) %>%
  select(gvkey_govtrack_id, gvkey, govtrack_id, year, naics, naics3, naics2, chamber, 
         lob_any_bin, lob_spon_bin, lob_cosp_bin, lob_comm_bin,
         don_any_bin, treat_don_all,
         don_tot, l_don_tot,
         l_sale, l_emp, # firm covariates
         majority, democrat, com_chair, power_com) %>% # recodings
  mutate(gvkey_govtrack_id_numeric = as.integer(as.factor(gvkey_govtrack_id)),
         year = as.integer(year),
         l_treat_don_all = ifelse(treat_don_all == 1, l_don_tot, 0)) 

gc()
gc()


# Regression models, binary donation indicator, first donation ever
lm1 <- lfe::felm(lob_any_bin ~ treat_don_all + majority + democrat + com_chair + power_com | gvkey | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm2 <- lfe::felm(lob_any_bin ~ treat_don_all + majority + democrat + com_chair + power_com | gvkey + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm3 <- lfe::felm(lob_any_bin ~ treat_don_all + majority + democrat + com_chair + power_com | gvkey + year + govtrack_id | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm4 <- lfe::felm(lob_any_bin ~ treat_don_all + majority + democrat + com_chair + power_com | gvkey_govtrack_id + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
gc()



# Regression models, logged donation account, first donation ever
lm5 <- lfe::felm(lob_any_bin ~ l_treat_don_all + majority + democrat + com_chair + power_com | gvkey | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm6 <- lfe::felm(lob_any_bin ~ l_treat_don_all + majority + democrat + com_chair + power_com | gvkey + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm7 <- lfe::felm(lob_any_bin ~ l_treat_don_all + majority + democrat + com_chair + power_com | gvkey + year + govtrack_id | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm8 <- lfe::felm(lob_any_bin ~ l_treat_don_all + majority + democrat + com_chair + power_com | gvkey_govtrack_id + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
gc()



stargazer::stargazer(lm1, lm2, lm3, lm4, lm5, lm6, lm7, lm8, type = "latex", title = "Firm and Employee Donations and Lobbying of Specific Politicians: The level of analysis is the firm-politician-year, and the data include all publicly traded manufacturing firms (NAICS 2-digit codes 31-33) and members of Congress between 2008 and 2017. The dependent variable is a dummy which equals 1 if firm $i$ lobbied the legislative activity of a politician $j$ in year $t$. $Donationt$ equals 1 if firm $i$ donated to a politician $j$ for the first time in year $t$, and 0 otherwise, whereas $Donation Amount_{t}$ is the logged amount of the first-time donation. The coefficients come from ordinary least squares regressions, using combinations of firm, politician, year, and firm-politician fixed effects. All models control for politician partisanship, majority status, committee chairmanship, and being member of the top 4 powerful committees. The standard errors are clustered by firm.",
                     label = "tab_h3",
                     out = paste0(FIG_DIR, "table_h3.tex"),
                     covariate.labels = c("$Donation_{t}$","$Donation\\;Amount_{t}$",
                                          "$Majority_{t}$", "$Democrat_{t}$", 
                                          "$Committee\\;Chair_{t}$", "$Powerful\\;Committee_{t}$"), 
                     dep.var.labels = c("Lobbying Legislative Activity$_{t}$"),
                     add.lines = list(
                       c("Firm FEs",  rep(c("$\\checkmark$", "$\\checkmark$", "$\\checkmark$", ""), 2)),
                       c("Year FEs",  rep(c("", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"), 2)),
                       c("Politician FEs",rep(c("", "", "$\\checkmark$", ""), 2)),
                       c("Firm-Politician FEs",rep(c("", "", "", "$\\checkmark$"), 2))),
                     omit.stat = c("rsq", "ser"),
                     column.sep.width = "3pt", notes.align = "l")

# substantive interpretation of donation amount on lobbying probability
log(1.1)*coef(lm8)["l_treat_don_all"] # 10% increase in donations is 0.00033 increase in prob, i.e 0.03pp

rm(lm1, lm2, lm3, lm4, lm5, lm6, lm7, lm8)
gc()







